#ifndef _VULKAN_BASE_PASS_H_
#define _VULKAN_BASE_PASS_H_

namespace Vulkan {
	namespace Pass{
		inline std::vector<VkSubpassDependency> getSubpassDependency() {
			return{
				{ VK_SUBPASS_EXTERNAL,
				0,
				VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
				VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
				VK_ACCESS_MEMORY_READ_BIT,
				VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
				VK_DEPENDENCY_BY_REGION_BIT },
				{ 0,
				VK_SUBPASS_EXTERNAL,
				VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
				VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
				VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
				VK_ACCESS_MEMORY_READ_BIT,
				VK_DEPENDENCY_BY_REGION_BIT
				}
			};
		}

		inline std::vector<VkAttachmentDescription> getRenderPassAttachments() {
			return{
				{ 0,
				context.colorFormat,
				VK_SAMPLE_COUNT_1_BIT,
				VK_ATTACHMENT_LOAD_OP_CLEAR,
				VK_ATTACHMENT_STORE_OP_STORE,
				VK_ATTACHMENT_LOAD_OP_DONT_CARE,
				VK_ATTACHMENT_STORE_OP_DONT_CARE,
				VK_IMAGE_LAYOUT_UNDEFINED,
				VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
				},
				{ 0,
				context.depth.format,
				VK_SAMPLE_COUNT_1_BIT,
				VK_ATTACHMENT_LOAD_OP_CLEAR,
				VK_ATTACHMENT_STORE_OP_DONT_CARE,
				VK_ATTACHMENT_LOAD_OP_DONT_CARE,
				VK_ATTACHMENT_STORE_OP_DONT_CARE,
				VK_IMAGE_LAYOUT_UNDEFINED,
				VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
				}
			};
		}

		inline VkRenderPass createBasePass() {
			VkAttachmentReference subPassColor = { 0,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
			VkAttachmentReference subPassDepth = { 1,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL };
			VkSubpassDescription subPass = {};
			subPass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
			subPass.colorAttachmentCount = 1;
			subPass.pColorAttachments = &subPassColor;
			subPass.pDepthStencilAttachment = &subPassDepth;
			subPass.inputAttachmentCount = 0;
			subPass.preserveAttachmentCount = 0;

			// Subpass dependencies for layout transitions
			auto dependencies = getSubpassDependency();
			auto attachments = getRenderPassAttachments();

			VkRenderPassCreateInfo info = {};
			info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
			info.attachmentCount = (uint32_t)attachments.size();
			info.pAttachments = attachments.data();
			info.subpassCount = 1;
			info.pSubpasses = &subPass;
			info.dependencyCount = (uint32_t)dependencies.size();
			info.pDependencies = dependencies.data();

			VkRenderPass pass = VK_NULL_HANDLE;
			vkCreateRenderPass(context.device, &info, nullptr, &pass);
			return pass;
		}
	}
}

#endif
